package leetcode.editor.cn.q1_300.q100;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author qidi
 * @date 2022-01-18 15:45:32
 */
class Q0150_EvaluateReversePolishNotation {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int evalRPN(String[] tokens) {
            Deque<Integer> stack = new ArrayDeque<>();
            for (int i = 0; i < tokens.length; i++) {
                switch (tokens[i]) {
                    case "+":
                        stack.push(stack.pop() + stack.pop());
                        break;
                    case "-":
                        int num1 = stack.pop();
                        stack.push(stack.pop() - num1);
                        break;
                    case "*":
                        stack.push(stack.pop() * stack.pop());
                        break;
                    case "/":
                        int num = stack.pop();
                        stack.push(stack.pop() / num);
                        break;
                    default:
                        stack.push(Integer.valueOf(tokens[i]));
                        break;
                }
            }
            return stack.pop();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0150_EvaluateReversePolishNotation().new Solution();
        System.out.println(solution.evalRPN(new String[]{"4", "3", "-"}));
        System.out.println(solution.evalRPN(new String[]{"2", "1", "+", "3", "*"}));
        System.out.println(solution.evalRPN(new String[]{"4", "13", "5", "/", "+"}));
        System.out.println(solution.evalRPN(new String[]{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}));
    }
}